package com.lmf.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmf.eduservice.entity.EduTeacher;
import com.lmf.eduservice.entity.vo.TeacherQuery;
import com.lmf.eduservice.service.EduTeacherService;
import com.lmf.resultCode.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author lmf
 * @since 2022-01-15
 */
@Slf4j
@RestController
@RequestMapping("/eduservice/edu-teacher")
@Api(description="讲师管理")
@CrossOrigin    //解决跨域问题
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 获取所有讲师对象集合
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查找所有讲师")
    public R findAllTeachers(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    @DeleteMapping("/deleteTeacherById/{id}")
    @ApiOperation(value = "根据id逻辑删除讲师")
    public R deleteTeacherById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable("id") String id){
        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return R.ok();
        }else {
            return R.error();
        }

    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/page/{current}/{limit}")
    public R pageTeacher(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable Long current,
                         @ApiParam(name = "limit", value = "每页显示记录数", required = true) @PathVariable Long limit){

        Page<EduTeacher> page = new Page<>(current,limit);
        //这个方法底层，会自动将数据封装到page这个对象中去
        eduTeacherService.page(page,null);
        long total = page.getTotal();
        return R.ok().data("total",total).data("page",page);
    }

    @ApiOperation(value = "按条件进行分页查询")
    @PostMapping("/pageTeacherCondition/{current}/{size}")
    public R pageTeacherCondition(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable("current") Long current,
                                  @ApiParam(name = "size", value = "每页显示记录数", required = true) @PathVariable("size") Long size,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        
        Page<EduTeacher> page = new Page<>(current,size);
        //从封装类中获取前台传过来的条件数据
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //将前端条件数据，封装为一个QueryWrapper条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_modified",end);
        }
        //添加排序条件：按照创建时间排序
        queryWrapper.orderByDesc("gmt_create");
        //进行分页查询
        eduTeacherService.pageMaps(page,queryWrapper);
        return R.ok().data("total",page.getTotal()).data("page",page).data("rows",page.getRecords());
    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher teacher){
        boolean save = eduTeacherService.save(teacher);
        if (save) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("根据id查找讲师")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable("id") String id){
//        int ins = 1/0;
//        log.error("异常");
        EduTeacher byId = eduTeacherService.getById(id);
        return R.ok().data("teacher",byId);
    }


    @ApiOperation("根据id修改讲师信息")
    @PostMapping("/updateTeacherById")
    public R updateByEntity(@RequestBody EduTeacher teacher){
        boolean flag = eduTeacherService.updateById(teacher);
        if (flag) {
            return R.ok();
        }else {
            return R.error();
        }
    }

}

